package com.rusamaha.gm.services;

import com.rusamaha.gm.dao.ProductRepository;
import com.rusamaha.gm.model.ProductBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
* Created by Mary on 23.02.2015.
*/
@Service
@Transactional
public class ProductService {

    @Autowired
    private ProductRepository productDao;

    public Iterable<ProductBase> getAllProducts(){
        return productDao.findAll();
    }

    public ProductBase saveOrUpdate(ProductBase product){
        if (product.getId() != null) {
            ProductBase dbProduct = productDao.findOne(product.getId());
//            if (task.getTaskComplexities() != null && !product.getTaskComplexities().isEmpty()) {
//                for (TaskComplexity iTaskC : task.getTaskComplexities()) {
//                    log.debug("USER: " + iTaskC.getAuthorComplexity().getId());
//                }
//            }
//            dbTask.setTaskComplexities(task.getTaskComplexities());
//            dbTask.setSolutions(task.getSolutions());
            productDao.save(dbProduct);
            return dbProduct;
        } else {
         /*   if (task.getTaskComplexities() != null && !task.getTaskComplexities().isEmpty()) {
                for (TaskComplexity iTaskC : task.getTaskComplexities()) {
                    if (iTaskC != null && iTaskC.getTask()!=null)
                        log.debug("TASK: " + iTaskC.getTask().getId());
                    else log.debug("Task not mapped");
                }
            }*/
            productDao.save(product);
            return product;
        }
    }

    public ProductBase findById(Long id){
        return productDao.findOne(id);
    }





}
